package com.flower.controller;
import com.flower.constant.JwtClaimsConstant;
import com.flower.model.dto.LoginDto;
import com.flower.model.entity.Login;
import com.flower.model.vo.LoginVo;
import com.flower.model.entity.SysMenu;
import com.flower.properties.JwtProperties;
import com.flower.result.Result;
import com.flower.service.UserService;
import com.flower.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("all")
/**
 *@ClassName UserController
 *@Description TODO
 *@Author @O_o  GW__
 *@Date 2025/6/10 16:18
 *@Version 1.0
 */
@RestController
@RequestMapping("/manager")
@Slf4j
@Api(tags = "用户管理")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result login(@RequestBody LoginDto loginDto){
        Login login =userService.login(loginDto);
        List<SysMenu> menus =userService.getMenus();
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, login.getAdid());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        LoginVo loginVO = LoginVo.builder()
                .id(login.getAdid())
                .adname(login.getAdname())
                .menus(menus)
                .token(token)
                .build();

        return Result.success(loginVO);
    }
}
